# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.GeneralOperation.ModuleOperation import moduleOper
from selenium.webdriver.support.ui import WebDriverWait
from pykeyboard import PyKeyboard


class orderStudent(lbtlogin):
    '''幼儿订购明细相关用例
    '''

    @lbtlogin.catchfun
    def test_export(self):
        '''进入模块，然后导出功能---------需要校验文件是否下载成功
        思路--1、通过对比下载前后下载路径下文件数量。
        2、校验新下载文件名及格式。
        3、校验新下载文件excel的头部信息
        4、校验新下载文件内容行数和查询行数是否一致。
        '''
        self.login()
        dr = self.driver
        mu = moduleOper(dr)
        k = PyKeyboard()
        # 进入模块
        self.assertTrue(mu.openMoudleAndIn('icon-order-child', '/ord/getStudentOrdList.jhtml', 'icon-menu-child'))
        dr.find_element_by_css_selector('button.btn.btn-default.btn-w-xs.export').click()
        if self.currentBrowser == 'firefox':
            # 操作键盘tab键和enter键进行模拟文件下载
            k.tap_key(k.tab_key, 3, 1)
            k.tap_key(k.enter_key)

    @lbtlogin.catchfun
    def test_queryByphone_and_orderA(self):
        '''通过手机号查询未订购的，然后订购A套餐'''
        self.docstr = "通过手机号查询未订购的，然后订购A套餐"
        self.login()
        dr = self.driver
        mu = moduleOper(dr)
        # 进入模块
        self.assertTrue(mu.openMoudleAndIn('icon-order-child', '/ord/getStudentOrdList.jhtml', 'icon-menu-child'))
        # 选择未订购
        dr.find_element_by_id("opercode_chosen").click()
        dr.find_element_by_xpath(
            '//div[@id="opercode_chosen"]/div/ul/li[@class="active-result" and @data-option-array-index="2"]').click()
        # 遍历手机号进行查询，如果都没有查询结果，就不输入手机号查询
        isHaveResult = False
        for phone in self.receiverPhoneForTest:
            dr.find_element_by_id('phone').clear()
            dr.find_element_by_id('phone').send_keys(phone)
            dr.find_element_by_class_name("form-inline").find_element_by_css_selector('button.btn.btn-primary').click()
            isHaveResult = dr.find_element_by_xpath("//tbody/tr/td[1]").text != '没有查询结果'
            if isHaveResult:
                break
        if not isHaveResult:
            dr.find_element_by_id('phone').clear()
            dr.find_element_by_class_name("form-inline").find_element_by_css_selector('button.btn.btn-primary').click()
        studentName = dr.find_element_by_xpath('//tbody/tr[1]/td[2]').text
        studentCalss = dr.find_element_by_xpath('//tbody/tr[1]/td[3]').text
        studentCalss = studentCalss.split('(')[0]
        dr.find_elements_by_css_selector("button.btn-init.text-primary.childOrder")[0].click()
        # 验证弹窗是否正常打开
        self.assertTrue(dr.find_element_by_css_selector("div.layui-layer.layui-layer-page.layer-anim").is_displayed())
        # 点击订购
        dr.find_element_by_xpath("//input[@name='orderPlan' and @value='K']").click()
        dr.find_element_by_class_name("layui-layer-btn0").click()
        # 校验是否订购中
        WebDriverWait(dr, 10).until(
            lambda dr: dr.find_element_by_id('childOrder').value_of_css_property('display') == 'none')
        dr.find_element_by_id('eduIds_chosen').click()
        # dr.find_element_by_xpath(
        # '//div[@id="eduIds_chosen"]/div/div/input').send_keys(studentCalss)
        classList = dr.find_elements_by_xpath(
            '//div[@id="eduIds_chosen"]/div/ul/li[@class="active-result"]')
        for ele in classList:
            if ele.text == studentCalss:
                ele.click()
                break
        dr.find_element_by_id('userName').send_keys(studentName)
        dr.find_element_by_id("opercode_chosen").click()
        dr.find_element_by_xpath(
            '//div[@id="opercode_chosen"]/div/ul/li[@class="active-result" and @data-option-array-index="3"]').click()
        dr.find_element_by_class_name("form-inline").find_element_by_css_selector('button.btn.btn-primary').click()
        isHaveResult = dr.find_element_by_xpath("//tbody/tr/td[1]").text != '没有查询结果'
        self.assertTrue(isHaveResult)
